/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Form;

import Boundary.Presensi;
//import java.s.Statement;
import Boundary.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author FaveGanteng
 */
public class LoginControl {

    private Connection conn;

    LoginControl(Connection KoneksiDataBase) {
        this.conn = KoneksiDataBase;
    }

    public static LoginControl getKoneksi() throws SQLException {
        LoginControl kon = new LoginControl(Tools.DataBaseConnection.KoneksiDataBase());
        return kon;
    }

    public void InsertPresensi(Presensi p) throws SQLException {
        // p = new User();
        String nis = p.getNis().getUsername();
        String tgl = p.getTanggal();
        int kode = 0;
        kode = kode + p.getKodekehadiran();

        String query = "insert into rpltpresensi "
                + "values ('" + tgl + "','" + nis
                + "','" + kode + "')";

        Statement stat = conn.createStatement();
        stat.executeUpdate(query);
    }

    public User cariSiswa(String username, String passwd) throws SQLException {
        PreparedStatement psmt = null;
        ResultSet reset = null;
        conn.setAutoCommit(false);

//        String username = u.getUsername();
//        String passwd = u.getPassword();
        String sql = "select * from rpltuserlogin "
                + " where nis ='" + username + "' and password = '" + passwd + "'";
//                + "' and password like '" + passwd + "'" ;

        psmt = conn.prepareStatement(sql);
        reset = psmt.executeQuery();

        User user = null;
        if (reset.next()) {
            user = new User();
            user.setUsername(reset.getString("nis"));
            user.setPassword(reset.getString("password"));
            user.setStatus(reset.getString("status"));
        }
        conn.commit();
        return user;
    }

    public User cariStatus(String masukan) throws SQLException {
        PreparedStatement psmt = null;
        ResultSet reset = null;
        String stat = null;
        conn.setAutoCommit(false);
//        String username = u.getUsername();
//        String passwd = u.getPassword();
//        String status = u.getStatus();
        String sql = "select status from rpltuserlogin where nis = '" + masukan + "'";
        //  System.out.println(username);
        psmt = conn.prepareStatement(sql);
        reset = psmt.executeQuery();

        User user = null;
        if (reset.next()) {
            user = new User();
            user.setUsername(reset.getString("nis"));
            user.setPassword(reset.getString("password"));
            user.setStatus(reset.getString("status"));
        }
        return user;
    }

    public void update(Presensi p) throws SQLException {
//        String Nis = tu.getNis().getUsername();
//        String tgl = tu.getTanggal();
//        System.out.println(tgl);
//        System.out.println(Nis);
//
//        String sql = "update rpltpresensi set kodekehadiran = 1 "
//                + " where nis = "+Nis+""
//                + " and tanggal = '"+tgl+"'";
//        Statement stat = conn.createStatement();
//        stat.executeUpdate(sql);
        String tgl = p.getTanggal();
        String nis = p.getNis().getUsername();
        int kode = 1;
        String sql = "update rpltpresensi"
                + " set kodekehadiran = "+kode
                + " where nis = '"+nis+"'"
                + " and tanggal = '"+tgl+"'";
        Statement stat = conn.createStatement();
        stat.executeUpdate(sql);
    }
//      public static void main(String [] args) {
//          Scanner s = new Scanner(System.in);
//
//          User u = new User();
//
//
//        try {
//           u.getUsername();
//           u.getPassword();
//
//            LoginControl.getKoneksi().cariSiswa(u);
//            System.out.println(u.getUsername());
//            System.out.println(u.getPassword());
//              //  System.out.println(u.getStatus());
//        } catch (SQLException ex) {
//            Logger.getLogger(LoginControl.class.getName()).log(Level.SEVERE, null, ex);
//        }
//      }
}
